<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/Welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 分栏间隔 -->
      <el-row :gutter="10">
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <!-- 搜索与添加 -->
            <div>
              <el-input placeholder="请输入内容" v-model="goods.query">
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="searchGoods()"
                ></el-button>
              </el-input>
            </div>
          </div>
        </el-col>
        <!-- 添加 -->
        <el-col :span="6">
          <div class="grid-content bg-purple">
            <el-button type="primary" round @click="goAdd()"
              >添加用户</el-button
            >
          </div>
        </el-col>
      </el-row>
      <!-- table栏 -->
      <el-table :data="goodsList" border style="width: 100%">
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100">
        </el-table-column>

        <el-table-column prop="goods_weight" label="商品重量" width="100">
        </el-table-column>

        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGoods(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="goods.pagesize"
        layout="total, prev, pager, next"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import http from '../util/http'
export default {
  data () {
    return {
      // 查询的数据绑定
      goods: {
        query: '', // 查找的值
        pagenum: 1, // 当前页码
        pagesize: 5 // 一页显示多少条数据
      },
      // table栏的数据
      goodsList: [],
      // table的总条数
      total: 1
    }
  },
  mounted () {
    // 调用获取数据
    this.getGoodsList()
  },
  methods: {
    // 获取数据
    getGoodsList () {
      http('goods', { params: this.goods }).then((res) => {
        console.log(res.data.data)
        this.total = res.data.data.total
        this.goodsList = res.data.data.goods
      })
    },
    // 搜索按钮
    searchGoods () {
      this.getGoodsList()
    },
    // 分页点击
    handleCurrentChange (val) {
      // console.log(val);

      this.goods.pagenum = val
      this.getGoodsList()
    },
    // 删除数据
    async delGoods (row) {
      const ref = await this.$confirm(
        '此操作将永久删除此分类, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((error) => error)

      if (ref === 'confirm') {
        http.delete(`goods/${row.goods_id}`).then((res) => {
          console.log()
          if (res.data.meta.status === 200) {
            this.$message.success('删除成功')
            this.getGoodsList()
          } else {
            this.$message.error('删除失败')
          }
        })
      }
    },
    // 跳转添加的页面
    goAdd () {
      this.$router.push('/add')
    }
  }
}
</script>
